package com.woniuxy.controller;

import com.woniuxy.dto.MyDto;
import com.woniuxy.pojo.MerchantCode;
import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.Note;
import com.woniuxy.vo.ChangepassVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping("/login.do")
    @ApiOperation(value = "登陆",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "登陆")
    @ResponseBody
    public MyDto login(@ApiParam(required = true,name = "telephone",value = "手机号")String telephone,
                       @ApiParam(required = true,name = "password",value = "密码")String password,
                       HttpServletRequest request){

        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(telephone,password);
        try {
            subject.login(token);
            //获取Session对象
            Session session=subject.getSession();
            User user=userService.findUserByTel(telephone);
            session.setAttribute("USER_SESSION",user);
            return MyDto.loginDtoSuccess;
        }catch (AuthenticationException e){

            e.printStackTrace();
            System.out.println(MyDto.loginDtoFail);
            return MyDto.loginDtoFail;
        }
    }

    /**
     * 生成验证码
     * @param telephone
     * @return
     */
    @GetMapping("/forgett.do")
    @ApiOperation(value = "生成验证码",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "生成验证码")
    @ResponseBody
    public MyDto forgett(@ApiParam(required = true,name = "telephone",value = "手机号") String telephone){
        //生成验证码
        String code=(int)(Math.random()*1000000)+"";

      try {
          MerchantCode merchantCode=new MerchantCode();
          merchantCode.setCode(code);
          merchantCode.setCreatetime(new Date());
          merchantCode.setTelephone(telephone);
          //往数据库插入数据
          userService.addcode(merchantCode);
          //发送短信，短信在我们的工具类里面，直接使用
          Note note=new Note();
          note.SMSTest(telephone,code);//发送验证码短信的一个方法
          return new MyDto(200,"验证码发送成功");
      }catch (Exception e){
          e.printStackTrace();
          return new MyDto(-200,"验证码发送失败");
      }
    }

    /**
     * 前端获取验证码
     * @return
     */
    @GetMapping ("/inputCode.do")
    @ApiOperation(value = "获取验证码",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "获取验证码")
    @ResponseBody
    public MyDto inputCode(@ApiParam(required = true,name = "telephone",value = "手机号") String telephone){
        MyDto myDto=new MyDto();
     try {
         String code=userService.findCodeBytelephone(telephone);
         myDto=  new MyDto(200,code);
     }catch (Exception e){
         e.printStackTrace();
         myDto.setCode(-200);
     }
        return myDto  ;
    }
    @PostMapping("/changepassworld.do")
    @ApiOperation(value = "修改密码",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = MyDto.class,notes = "修改密码")
    @ResponseBody
    private MyDto changepassworld(ChangepassVo changepassVo){
        MyDto myDto=new MyDto();

        try{
            String password=changepassVo.getPassword();
            String telephone=changepassVo.getTelephone();
            String algorithmName = "md5";
            Object salt = null;
            int hashIterations = 1024;
            Object result = new SimpleHash(algorithmName, password, salt, hashIterations);
            String x = String.valueOf(result);
            password=x;
            userService.uppassword(telephone,password);
            myDto.setCode(200);
            myDto.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            myDto.setCode(-200);
            myDto.setMsg("修改失败");

        }
        return myDto;
    }



}
